package com.qianfeng.smartdevice.service.impl;

import com.qianfeng.smartdevice.cache.CategoryCache;
import com.qianfeng.smartdevice.cache.SceneCache;
import com.qianfeng.smartdevice.ebums.CheckType;
import com.qianfeng.smartdevice.exception.AddDataErrorException;
import com.qianfeng.smartdevice.exception.MyErrorCode;
import com.qianfeng.smartdevice.exception.QueryDataErrorException;
import com.qianfeng.smartdevice.mapper.SysDeviceMapper;
import com.qianfeng.smartdevice.pojo.SysCategory;
import com.qianfeng.smartdevice.pojo.SysDevice;
import com.qianfeng.smartdevice.pojo.SysScene;
import com.qianfeng.smartdevice.pojo.SysUserInfo;
import com.qianfeng.smartdevice.service.SysDeviceService;
import com.qianfeng.smartdevice.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.util.Date;
/**
 * @time: 2022-03-22 19:50
 */

@Service
@Transactional
public class SysDeviceServiceImpl implements SysDeviceService {
    private CategoryCache categoryCache;
    @Autowired
    public void setCategoryCache(CategoryCache categoryCache) {
        this.categoryCache = categoryCache;
    }

    private SceneCache sceneCache;
    @Autowired
    public void setSceneCache(SceneCache sceneCache) {
        this.sceneCache = sceneCache;
    }

    private SysDeviceMapper sysDeviceMapper;
    @Autowired
    public void setSysDeviceMapper(SysDeviceMapper sysDeviceMapper) {
        this.sysDeviceMapper = sysDeviceMapper;
    }

    @Override
    public void addSysDevice(SysDevice device) {
        //需要对数据进行校验
        Assert.isTrue(!device.isEmpty(CheckType.ADD),()->{
            throw new AddDataErrorException("数据不能为空", MyErrorCode.DATA_NULL);
        });
        //登录验证
        //我们要求必须登录
        SysUserInfo userInfo = SecurityUtils.getLongUser();
        Assert.notNull(userInfo,()->{
            throw new AddDataErrorException("未登录",MyErrorCode.NOT_LOGIN);
        });
        device.setBindUserId(userInfo.getUId());//这个用户一定是有值的
        //检查用户是否绑定了
        SysDevice sysDevice = sysDeviceMapper.findById(device.getDeviceId());
        Assert.isNull(sysDevice,()->{
            throw new AddDataErrorException("设备已经被绑定",MyErrorCode.DEVICE_ALLREADY_BIND);
        });
        //检查场景是不是空的,是空的就设置为-1,不是空的就检查这个场景属不属于当前的用户,因为不能添加到别人的场景中
        if(device.getSceneId()==null){
            device.setSceneId(-1L);
        }else if(device.getSceneId()!=-1){
            //传递了场景ID,但不是-1,需要检查这个场景的ID是不是属于当前用户
            //我们有了场景的缓存,可以直接从缓存中查询这个场景的数据
            SysScene sysScene = sceneCache.get(device.getSceneId());
            //这个场景可能不存在,也可能不属于当前用户,但不管什么情况,只要不符合,我们就提示差根据不存在
            Assert.isTrue(sysScene!=null&&sysScene.getCreateBy()==userInfo.getUId(),()->{
                throw new AddDataErrorException("场景不存在",MyErrorCode.SCENE_NOT_EXIST);
            });
        }
        //检查分类是不是存在,分类首先必须存在,但是可以是状态为0被删除状态,这是我们的业务要求,我们当前项目是允许添加被删除的分类设备
        SysCategory category = categoryCache.getWithDelete(device.getCategyId());
        Assert.notNull(category,()->{
            throw new AddDataErrorException("分类不能为空",MyErrorCode.CATEGORY_NOT_EXIST);
        });
        device.setBindTime(new Date());
        sysDeviceMapper.addSysDevice(device);

    }

    @Override
    public SysDevice findById(String deviceId) {
        //登录验证
        //我们要求必须登录
        SysUserInfo userInfo = SecurityUtils.getLongUser();
        Assert.notNull(userInfo,()->{
            throw new QueryDataErrorException("未登录",MyErrorCode.NOT_LOGIN);
        });
        SysDevice sysDevice = sysDeviceMapper.findById(deviceId);
        Assert.isTrue(sysDevice==null||userInfo.getUId()==sysDevice.getBindUserId(),()->{
            throw new QueryDataErrorException("设备不存在",MyErrorCode.DEVICE_NOT_EXIST);
        });
        return sysDevice;
    }
}
